/*
 * @Author: 菜鸟裹裹 675687356@qq.com
 * @Date: 2025-06-10 09:27:22
 * @LastEditors: 菜鸟裹裹 675687356@qq.com
 * @LastEditTime: 2025-06-17 15:09:32
 * @FilePath: /customer-service-web/src/store/modules/permission.js
 * @Description: 这是默认设置,请设置`customMade`, 打开koroFileHeader查看配置 进行设置: https://github.com/OBKoro1/koro1FileHeader/wiki/%E9%85%8D%E7%BD%AE
 */
import { publicRoutes, privateRoutes } from '@/router/index.js'

export default {
  namespaced: true,
  state: {
    // 路由表：初始拥有静态路由权限
    routes:[]
  },
  
  plugins: [store => {
    // 插件会在 store 初始化后执行
    store.state.routes = publicRoutes;
    
  }],
  mutations: {
    /**
     * 增加路由
     */
    setRoutes(state, newRoutes) {
      // 永远在静态路由的基础上增加新路由
      state.routes = [...publicRoutes, ...newRoutes]
    }
  },
  actions: {
    /**
     * 根据权限筛选路由
     */
    filterRoutes(context, menus) {
      const routes = []
      // 路由权限匹配
      menus.forEach(key => {
        // 权限名 与 路由的 name 匹配
        routes.push(...privateRoutes.filter(item => item.name === key))
        console.log(routes,"routesroutesroutes");
      })
      // 最后添加 不匹配路由进入 404
      routes.push({
        path: '/:catchAll(.*)',
        redirect: '/404'
      })
      context.commit('setRoutes', routes)
      return routes
    }
  }
}
